package com.zzj.portalwebsite.Dao.Impl;

import com.zzj.portalwebsite.Dao.ArticalInfoDao;

import com.zzj.portalwebsite.entity.article;
import com.zzj.portalwebsite.utlis.ConnectionDatabaseForMySQLUseJdbc;


import com.zzj.portalwebsite.utlis.StrConvertLocalDateTime;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ArticalInfoDaoImpl implements ArticalInfoDao {

    Connection connection = null;
    PreparedStatement pstmt = null;
    ResultSet resultSet = null;


    public ArticalInfoDaoImpl(){
        connection = ConnectionDatabaseForMySQLUseJdbc.getConnection();
        System.out.println(connection);
        System.out.println("嘻嘻");
    }


    @Override
    public List<article> getarticles(int pagenum) {
        List<article> list = Collections.synchronizedList(new ArrayList<>());
        if (connection == null){
            return null;
        }
        int a =(pagenum-1)*5;
        try {
            pstmt = connection.prepareStatement("select * from articles where status=1 limit 5 OFFSET "+a);
            resultSet = pstmt.executeQuery();
        while (resultSet.next()){
        list.add(new article(
                resultSet.getInt("id"),
                resultSet.getString("title"),
                resultSet.getString("content"),
                resultSet.getString("author"),
                StrConvertLocalDateTime.strToLocalDateTime(resultSet.getString("publish_time")),
                resultSet.getInt("status"),
                resultSet.getString("newsimg")));
            }

        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return list;
    }

    @Override
    public article getNewsById(int id) {
        if (connection == null) {
            return null;
        }
        article article= null;
        try {
            pstmt = connection.prepareStatement("select * from articles where id=?");
            pstmt.setInt(1, id);

            resultSet = pstmt.executeQuery();
            if (resultSet.next()) {
                article = new article();
                article.setId(resultSet.getInt("id"));
                article.setTitle(resultSet.getString("title"));
                article.setContent(resultSet.getString("content"));
                article.setAuthor(resultSet.getString("author"));
                article.setPublish_time(StrConvertLocalDateTime.strToLocalDateTime(resultSet.getString("publish_time")));
                article.setStatus(resultSet.getInt("status"));
                article.setNews_img(resultSet.getString("newsimg"));
            }


        } catch (SQLException e) {
            throw new RuntimeException(e);
        }

        return article;
    }

    @Override
    public boolean addNews(article news) throws SQLException {
        if (connection == null) {
            return false;
        }

        pstmt = connection.prepareStatement("insert into articles(title,content,author,publish_time,status,newsimg) values(?,?,?,?,?,?)");
        pstmt.setString(1, news.getTitle());
        pstmt.setString(2, news.getContent());
        pstmt.setString(3, news.getAuthor());
        pstmt.setTimestamp(4, new java.sql.Timestamp(System.currentTimeMillis()));
        pstmt.setInt(5, news.getStatus());
        pstmt.setString(6, news.getNewsimg());
        int i = pstmt.executeUpdate();


        return i==1 ? true : false;
    }



}


